package corsi.jpa.demo.jta.controller;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import corsi.jpa.demo.prima.domain.CD;

/**
 * Servlet implementation class FrontController
 */
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final Logger log= Logger.getLogger(FrontController.class);// = Logger.
   
	@PersistenceContext
	(unitName="cdUnitJta")
	private EntityManager entityManager;
	
	
	/**
     * Default constructor. 
     */
    public FrontController() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		log.info("doGet");		
		
		try{
			
			CD nuovo = new CD();
			nuovo.setArtist("Bob Dylan");
			nuovo.setTitle("Empire Burlesque");
			nuovo.setCompany("RCA");
			nuovo.setPrice(10.90);
			nuovo.setYear(1966);
			nuovo.setCountry("USA");
			nuovo.setQuantity(12);
			
			log.info("cd con id: "+nuovo.getPrimaryKey());
			entityManager.persist(nuovo);
			log.info("inserito con id: "+nuovo.getPrimaryKey());
					
						
			log.info(request.getPathTranslated()+"InserimentoOk.jsp");
			RequestDispatcher dispatcher =request.getRequestDispatcher(request.getContextPath()+"InserimentoOk.jsp");
			request.setAttribute("IdCd", nuovo.getPrimaryKey());
			dispatcher.forward(request, response);
		
		
		}
		catch (Exception e){
			
			log.info("http://localhost:8080/JPA-003-JTA/InserimentoErr.jsp");
			
			log.info(request.getContextPath()+"InserimentoErr.jsp");
			log.info(request.getPathTranslated()+"InserimentoErr.jsp");
			
			RequestDispatcher dispatcher =request.getRequestDispatcher("InserimentoErr.jsp");
			request.setAttribute("eccezione", e);
			dispatcher.forward(request, response);
		}
		
		
		
		
		
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}
	
	
	
	
	
	

}
